package com.systop.controller;

import com.systop.service.CaptchaService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * @author jsj
 * @date 2022/5/5
 */
@RestController
public class CaptchaController {

    @Autowired
    private CaptchaService captchaService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    /**
     * 验证码
     */
    @GetMapping("/captcha")
    // @ApiOperation("获取验证码")
    public void captcha(HttpServletResponse response, String uuid)throws IOException {



        if(uuid != "undefined" && uuid != ""){
            response.setHeader("Cache-Control", "no-store, no-cache");
            response.setContentType("image/jpeg");

            //获取图片验证码
            BufferedImage image = captchaService.getCaptcha(uuid);

            //转换成流形式传输到前台
            ServletOutputStream out = response.getOutputStream();
            ImageIO.write(image, "jpg", out);
            IOUtils.closeQuietly(out);
        }else{
            // TODO: 获取验证码出现不符合规则的请求处理方案
        }
    }
}
